import debounce from "lodash/debounce";
export default {
  data() {
    return {
      mixinsResizeFirstRuning: true,
      resizeDebounce: null,
    };
  },
  mounted() {
    this.resizeDebounce = debounce(() => {
      this.onResize && this.onResize();
    }, 100);
    this.$_initResizeEvent();
    this.$nextTick(() => {
      this.mixinsResizeFirstRuning = false;
    });
  },
  beforeDestroy() {
    this.$_destroyResizeEvent();
  },
  activated() {
    if (this.mixinsResizeFirstRuning) return;
    this.$_initResizeEvent();
  },
  deactivated() {
    this.$_destroyResizeEvent();
  },
  methods: {
    $_initResizeEvent() {
      this.onResize && this.onResize();
      window.addEventListener("resize", this.resizeDebounce);
    },
    $_destroyResizeEvent() {
      window.removeEventListener("resize", this.resizeDebounce);
    },
  },
};
